//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.RuntimeServices.Tracking
{
    using System;
    using System.Workflow.Runtime.Tracking;
    using Microsoft.Research.eResearch.RuntimeServices.Logging;

    public class VerboseTrackingChannel : TrackingChannel
    {
        private IServiceProvider serviceProvider = null;

        private TrackingParameters trackingParameters = null;

        public VerboseTrackingChannel(IServiceProvider serviceProvider, TrackingParameters trackingParameters)
        {
            this.serviceProvider = serviceProvider;
            this.trackingParameters = trackingParameters;
        }

        protected override void InstanceCompletedOrTerminated()
        {
            ILoggingService log = serviceProvider.GetLoggingService();
            log.Debug("InstanceCompletedOrTerminated, InstanceId: {0}", trackingParameters.InstanceId);            
        }

        protected override void Send(TrackingRecord record)
        {
            ILoggingService log = serviceProvider.GetLoggingService();

            ActivityTrackingRecord atr = record as ActivityTrackingRecord;
            if (atr != null)
            {
                log.Debug("ActivityTrackingRecord:");
                log.Debug(" ActivityType:{0}", atr.ActivityType);
                log.Debug(" ContextGuid:{0}", atr.ContextGuid);
                log.Debug(" EventDateTime:{0}", atr.EventDateTime.ToLongTimeString());
                log.Debug(" EventOrder:{0}", atr.EventOrder);
                log.Debug(" ExecutionStatus:{0}", atr.ExecutionStatus);
                log.Debug(" ParentContextGuid:{0}", atr.ParentContextGuid);
                log.Debug(" QualifiedName:{0}", atr.QualifiedName);

                if (atr.Annotations.Count > 0)
                {
                    log.Debug(" Annotations:");
                    foreach (string annotation in atr.Annotations)
                    {
                        log.Debug("  {0}", annotation);
                    }
                }

                if (atr.Body.Count > 0)
                {
                    log.Debug(" Body:");
                    foreach (TrackingDataItem extract in atr.Body)
                    {
                        log.Debug("  {0}:{1}", extract.FieldName, extract.Data);

                        if (extract.Annotations.Count > 0)
                        {
                            log.Debug("   Annotations:");
                            foreach (string annotation in extract.Annotations)
                            {
                                log.Debug("    {0}", annotation);
                            }
                        }
                    }
                }
            }
            
            WorkflowTrackingRecord wtr = record as WorkflowTrackingRecord;
            if (wtr != null)
            {
                log.Debug("WorkflowTrackingRecord:");
                log.Debug(" EventDateTime:{0}", wtr.EventDateTime.ToLongTimeString());
                log.Debug(" EventOrder:{0}", wtr.EventOrder); 
                log.Debug(" TrackingWorkflowEvent:{0}", wtr.TrackingWorkflowEvent);

                if (wtr.Annotations.Count > 0)
                {
                    log.Debug(" Annotations:");
                    foreach (string annotation in wtr.Annotations)
                    {
                        log.Debug("  {0}", annotation);
                    }
                }
            }

            UserTrackingRecord utr = record as UserTrackingRecord;
            if (utr != null)
            {
                log.Debug("UserTrackingRecord:");
                log.Debug(" ActivityType:{0}", utr.ActivityType);
                log.Debug(" ContextGuid:{0}", utr.ContextGuid);
                log.Debug(" EventDateTime:{0}", utr.EventDateTime.ToLongTimeString());
                log.Debug(" EventOrder:{0}", utr.EventOrder);
                log.Debug(" ParentContextGuid:{0}", utr.ParentContextGuid);
                log.Debug(" QualifiedName:{0}", utr.QualifiedName);
                log.Debug(" UserDataKey:{0}", utr.UserDataKey);
                log.Debug(" UserData:{0}", utr.UserData);

                if (utr.Annotations.Count > 0)
                {
                    log.Debug(" Annotations:");
                    foreach (string annotation in utr.Annotations)
                    {
                        log.Debug("  {0}", annotation);
                    }
                }
                
                if (utr.Body.Count > 0)
                {
                    log.Debug(" Body:");
                    foreach (TrackingDataItem extract in utr.Body)
                    {
                        log.Debug("  {0}:{1}", extract.FieldName, extract.Data);

                        if (extract.Annotations.Count > 0)
                        {
                            log.Debug("   Annotations:");
                            foreach (string annotation in extract.Annotations)
                            {
                                log.Debug("    {0}", annotation);
                            }
                        }
                    }
                }
            }
        }
    }
}
